package com.ck.common.controller;

import com.ck.common.service.ModuleService;
import com.ck.constant.RequestConstant;
import com.ck.constant.ResponseConstant;
import com.ck.model.common.Module;
import com.ck.model.common.User;
import com.ck.model.common.sys.JsonResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @author dxy
 * @date 2018/6/22 10:23
 */
@Controller
@RequestMapping("/module")
public class ModuleController {
	private static Logger logger = LoggerFactory.getLogger(ModuleController.class);

	@Autowired
	private ModuleService moduleService;

	/**
	 * 角色模块列表(权限分配)
	 */
	@ResponseBody
	@RequestMapping("/listRoleModule")
	public Object listRoleModule() {
		return moduleService.listAllRoleModule();
	}

	/**
	 * 获取所有菜单
	 */
	@ResponseBody
	@RequestMapping("/listModuleTree")
	public Object listModuleTree(Long roleId, HttpServletRequest request) {
		Integer moduleType = null;
		User loginUser = (User) request.getSession().getAttribute(RequestConstant.SESSION_KEY_USER);
		// 判断当前登录用户类型 用户类型（0：创客用户；1：商家用户）
		Integer type = loginUser.getType();
		if(type != null && 1 == type) {
			// 商家用户则只能查询商家模块，创客用户可以查询所有模块
			moduleType = type;
		}
		return moduleService.getTreeList(roleId, moduleType);
	}

	/**
	 * 模块列表
	 */
	@RequestMapping(value = "/list")
	public String goModuleListPage() {
		return "common/module_list";
	}

	/**
	 * 保存模块
	 */
	@ResponseBody
	@RequestMapping(value = "/save")
	public Object saveModule(Module module, HttpServletRequest request) {
		if (module == null) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "输入参数不能为空");
		}
		if (org.apache.commons.lang3.StringUtils.isBlank(module.getModuleName())) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "资源名称不能为空");
		}
		try {
			//检查名称是否重复
			Module nameModule = moduleService.getModuleByModuleName(module.getModuleName(), module.getModuleType());
			if (nameModule != null) {
				return new JsonResponse(ResponseConstant.CODE_FAIL, "资源名称已存在");
			}
			//保存资源
			moduleService.saveModule(module, request);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("ModuleController saveModule 保存模块失败：" + e.getMessage());
			return e;
		}
		return "";
	}

	/**
	 * 修改模块
	 */
	@ResponseBody
	@RequestMapping(value = "/update")
	public Object updateModule(Module module, HttpServletRequest request) {
		if (module == null) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "输入参数不能为空");
		}
		String moduleName = module.getModuleName();
		if (org.apache.commons.lang3.StringUtils.isBlank(moduleName)) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "资源名称不能为空");
		}
		try {
			//检查名称是否重复
			Module nameModule = moduleService.getModuleByModuleName(moduleName, module.getModuleType());
			if (nameModule != null) {
				if (!moduleName.equals(nameModule.getModuleName())) {
					return new JsonResponse(ResponseConstant.CODE_FAIL, "资源名称已存在");
				}
			}
			//修改模块
			moduleService.updateModuleById(module, request);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("ModuleController updateModule 修改模块失败：" + e.getMessage());
			return e;
		}
		return "";
	}

	/**
	 * 删除模块
	 */
	@ResponseBody
	@RequestMapping(value = "/delete")
	public Object deleteModule(Long moduleId) {
		if (moduleId == null) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "模块ID不能为空，请选择模块");
		}
		try {
			moduleService.deleteModuleById(moduleId);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("ModuleController deleteModule 删除模块失败：" + e.getMessage());
			return e;
		}
		return "";
	}

	/**
	 * 获取当前登录用户所拥有的菜单模块
	 */
	@ResponseBody
	@RequestMapping(value = "/listMenuModule")
	public Object listMenuModule(HttpServletRequest request) {
		User loginUser = (User) request.getSession().getAttribute(RequestConstant.SESSION_KEY_USER);
		if (loginUser == null) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "不能获取当前登录用户，请联系管理员");
		}
		Long userId = loginUser.getUserId();
		if (userId == null) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "不能获取当前登录用户，请联系管理员");
		}
		try {
			return moduleService.listMenuModuleByUserId(userId);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("ModuleController listMenuModule 获取当前登录用户所拥有的菜单模块失败：" + e.getMessage());
			return e;
		}
	}

}
